package com.xzr.yespaper.controller;

import com.alibaba.fastjson.JSON;
import com.xzr.yespaper.bean.Comment;
import com.xzr.yespaper.bean.Paper;
import com.xzr.yespaper.service.CommentService;
import com.xzr.yespaper.service.PaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.configurationprocessor.json.JSONException;
import org.springframework.boot.configurationprocessor.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class CommentMainController {

    @Autowired
    CommentService commentService;

    @Autowired
    PaperService paperService;

    /**
     * 新增评论 Add comment
     * @param comment com.xzr.yespaper.bean.Comment
     * @return JSON {code: ture/false}
     * @throws JSONException（没用）
     */
    @PostMapping("/createcomment")
    @ResponseBody
    public String create(Comment comment) throws JSONException {
        // 设置提交时间 Update edit time
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        comment.setDate(time);
        try{
            commentService.creatComment(comment);
            // 检查当前论文的状态 Check the state og paper, if it can be published
            List<Comment> thisPaperComments = commentService.getCommentByPaperID(comment.getPaperid());
            if(thisPaperComments.size()>=2){
                int icount = 0;
                for(int i=0;i<thisPaperComments.size();i++){
                    if(thisPaperComments.get(i).getOk().equals("Pass")){
                        icount++;
                    }
                }
                if(icount>=2){
                    // 同意数2以上，更改为可发表 Check if it can be published (2 or more agree)
                    Paper p = new Paper();
                    p.setId(comment.getPaperid());
                    p.setState("publishable");
                    paperService.updatePaperState(p);
                }
            }
            //返回 return Json (ok)
            JSONObject ret = new JSONObject();
            ret.put("code",true);
            return ret.toString();
        } catch (JSONException e) {
            //返回 return Json (not ok)
            JSONObject ret = new JSONObject();
            ret.put("code",false);
            return ret.toString();
        }
    }

    /**
     * 修改评论 Edit comment
     * @param comment com.xzr.yespaper.bean.Comment
     * @return JSON {code: ture/false}
     * @throws JSONException（没用）
     */
    @PostMapping("/updatecomment")
    @ResponseBody
    public String update(Comment comment) throws JSONException {
        // 设置提交时间 Update edit time
        String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
        comment.setDate(time);
        try{
            commentService.updateCommentByID(comment);
            // 检查当前论文的状态 Check if it can be published (2 or more agree)
            List<Comment> thisPaperComments = commentService.getCommentByPaperID(comment.getPaperid());
            if(thisPaperComments.size()>=2){
                int icount = 0;
                for(int i=0;i<thisPaperComments.size();i++){
                    if(thisPaperComments.get(i).getOk().equals("Pass")){
                        icount++;
                    }
                }
                if(icount>=2){
                    // 同意数2以上，更改为可发表 Check if it can be published (2 or more agree)
                    Paper p = new Paper();
                    p.setId(comment.getPaperid());
                    p.setState("publishable");
                    paperService.updatePaperState(p);
                }
            }
            //返回 return Json (ok)
            JSONObject ret = new JSONObject();
            ret.put("code",true);
            return ret.toString();
        } catch (JSONException e) {
            //返回 return Json (not ok)
            JSONObject ret = new JSONObject();
            ret.put("code",false);
            return ret.toString();
        }
    }

    /**
     * 删除评论 Delete comment
     * @param comment com.xzr.yespaper.bean.Comment
     * @return JSON {code: ture/false}
     * @throws JSONException（没用）
     */
    @PostMapping("/deletecomment")
    @ResponseBody
    public String delete(Comment comment) throws JSONException {
        try{
            commentService.deleteCommentByID(comment.getId());
            JSONObject ret = new JSONObject();
            ret.put("code",true);
            return ret.toString();
        } catch (JSONException e) {
            JSONObject ret = new JSONObject();
            ret.put("code",false);
            return ret.toString();
        }
    }

    /**
     * 根据论文id得到论文的评论 Get comment by comment id
     * @param id Long 论文id comment id
     * @return JSON {code: false}/{code: true, comments: []}
     * @throws JSONException（没用）
     */
    @PostMapping("/getcomment")
    @ResponseBody
    public String get(@RequestParam("id") Long id) throws JSONException {
        try{
            List<Comment> comments = commentService.getCommentByPaperID(id);
            String list = JSON.toJSONString(comments);
            JSONObject ret = new JSONObject();
            ret.put("code",true);
            ret.put("comments",list);
            return ret.toString();
        } catch (JSONException e) {
            JSONObject ret = new JSONObject();
            ret.put("code",false);
            return ret.toString();
        }
    }
}
